

#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

bool is_not_space(char c)
{
    return c != ' ';
}

std::string trim_left(std::string s)
{
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), is_not_space));
    return s;
}

std::string trim_right(std::string s)
{
    s.erase(std::find_if(s.rbegin(), s.rend(), is_not_space).base(), s.end());
    return s;
}

int main()
{
    std::string s = "   hello functional programming C++!     ";
    std::cout << s << std::endl;

    std::cout << trim_left(trim_right(std::move(s))) << std::endl;

    std::vector<int> vec { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    std::vector<int> odd;
    std::copy_if(vec.cbegin(), vec.cend(), std::back_inserter(odd), [](int x) { return x % 2; });
    for (auto x : odd) {
        std::cout << x << ", ";
    }
    std::cout << std::endl;

    // remove_if算法只是把符合删除条件的集合放在开头部分
    vec.erase(std::remove_if(vec.begin(), vec.end(), [](int x) { return !(x % 2); }), vec.end());
    for (auto x : vec) {
        std::cout << x << ", ";
    }
    std::cout << std::endl;

    return 0;
}